package com.bocloud.cmp.server.scheduler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.bocloud.cmp.dao.HostPerfDao;
import com.bocloud.cmp.driver.zteadapter.HostResourceModel;
import com.bocloud.cmp.driver.zteadapter.ZTEHttpRequest;
import com.bocloud.cmp.driver.zteadapter.ZTEUrl;
import com.bocloud.cmp.entity.resource.CloudVendor;
import com.bocloud.cmp.entity.resource.HostPerf;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.interfaces.resource.CloudVendorService;
import com.bocloud.coordinator.cache.LeaderShip;
import com.google.common.collect.Lists;

/**
 * 
 * @author oywl
 *
 */
@Component
public class PerformSyncScheduler {

    @Autowired
    private CloudVendorService cloudVendorService;

    @Autowired
    private HostPerfDao hostPerfDao;

    @Value("${zx.openstack.isZXPlatform:false}")
    private String isZTEPlatform;

    @Autowired
    private ZTEUrl zteUrl;

    @Scheduled(cron = "${perf.sync.schedule:0 0/1 * * * ?}")
    protected void syncxz() throws Exception {
        if (!Boolean.parseBoolean(isZTEPlatform)) {
            return;
        }
        if (!(LeaderShip.isLeader())) {
            return;
        }
        List<CloudVendor> vendors = this.cloudVendorService.list(VendorType.OPENSTACK.name());
        for (CloudVendor vendor : vendors) {
            ZTEHttpRequest zxhttpRequest = new ZTEHttpRequest(zteUrl.getZxurl());

            List<HostPerf> hostPerfs = convertHostResoure(zxhttpRequest.getHostResource(), vendor.getId());
            if (null != hostPerfs) {
                for (HostPerf hostPerf : hostPerfs) {
                    hostPerfDao.save(hostPerf);
                }
            }
            // 这个接口暂时有问题，先注释掉 ouywl
            // List<HostPerf> hostPerfInstances =
            // convertInstanceResource(zxhttpRequest.getInstanceResource(),
            // vendor.getId());
            // if (null != hostPerfInstances) {
            // for (HostPerf hostPerf : hostPerfInstances) {
            // hostPerfDao.save(hostPerf);
            // }
            // }
        }

    }

    /**
     * 物理机转换
     * 
     * @param hostResourceModels
     * @param vendorId
     * @return
     */
    private List<HostPerf> convertHostResoure(List<HostResourceModel> hostResourceModels, Long vendorId) {
        if (null == hostResourceModels) {
            return null;
        }
        List<HostPerf> hostPerfs = Lists.newArrayList();
        for (HostResourceModel hostRM : hostResourceModels) {
            HostPerf hostPerf = new HostPerf();
            hostPerf.setCpuUtil(Float.parseFloat(hostRM.getCpu_util()));
            hostPerf.setMemUtil(Float.parseFloat(hostRM.getMemory_util()));
            hostPerf.setGmtCreate(hostRM.getDatetime());
            hostPerf.setHostName(hostRM.getName());
            hostPerf.setVendorId(vendorId);
            hostPerf.setIsGather(0);
            hostPerfs.add(hostPerf);
        }
        return hostPerfs;

    }

    /**
     * 虚拟机转换
     * 
     * @param instanceResourceModels
     * @param vendorId
     * @return
     */
//    private List<HostPerf> convertInstanceResource(List<InstanceResourceModel> instanceResourceModels, Long vendorId) {
//        if (null == instanceResourceModels) {
//            return null;
//        }
//        List<HostPerf> hostPerfs = Lists.newArrayList();
//        for (InstanceResourceModel instanceRM : instanceResourceModels) {
//            HostPerf hostPerf = new HostPerf();
//            hostPerf.setCpuUtil(Float.parseFloat(instanceRM.getCpu_util()));
//            hostPerf.setMemUtil(Float.parseFloat(instanceRM.getMemory_util()));
//            hostPerf.setGmtCreate(instanceRM.getDatetime());
//            hostPerf.setHostName(instanceRM.getName());
//            hostPerf.setVendorId(vendorId);
//            hostPerf.setNetworkIncoming(Float.parseFloat(instanceRM.getNetwork_incoming()));
//            hostPerf.setNetworkOutgoing(Float.parseFloat(instanceRM.getNetwork_outgoing()));
//            hostPerf.setIsGather(0);
//            hostPerfs.add(hostPerf);
//        }
//
//        return hostPerfs;
//    }

}